package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;


@Configuration
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0/1 * * * ?")
    //每小时从0分0秒开始,每隔1分钟执行一次
    public void dingshiTask(){
        Date date = new Date();
        LambdaUpdateWrapper<LeaseAgreement> a = new LambdaUpdateWrapper<>();
        //定时任务对象必须租约已经失效或者即将要失效,所以筛选出租约已经失效或者即将要失效的用户.截止时间<=目前时间,而且改的是状态处在2,5,7的,还没来得及改状态的用户.

        a.le(LeaseAgreement::getLeaseEndDate,date).in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING
        ,LeaseStatus.RENEWING).set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(a);


    }

}
